package com.wzw.shoppingcart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzw.shoppingcart.entity.CartLine;
import com.wzw.shoppingcart.entity.Discount;
import com.wzw.shoppingcart.entity.Promotion;
import com.wzw.shoppingcart.mapper.PromotionMapper;
import com.wzw.shoppingcart.service.DiscountService;
import com.wzw.shoppingcart.service.DiscountPromotionService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import static com.wzw.shoppingcart.constant.Constant.GLOBAL_DISCOUNT;
import static com.wzw.shoppingcart.constant.Constant.NOT_USEFUL;

/**
 *@author wzw
 * 2023/4/15
 */
@Service
@Slf4j
@Setter(onMethod_ = {@Autowired})
public class GlobalDiscountServiceImplDiscount extends ServiceImpl<PromotionMapper, Promotion> implements DiscountPromotionService {
    private DiscountService discountService;

    @Override
    public BigDecimal calculateDiscount(List<CartLine> cartLines, BigDecimal totalOriginPrice) {
        Discount discount = discountService.getByType(GLOBAL_DISCOUNT);
        if (NOT_USEFUL.equals(discount.getUseful())){
            return BigDecimal.ZERO;
        }
        BigDecimal conditionMoney = discount.getConditionMoney();
        BigDecimal discountMoney = discount.getDiscountMoney();
        BigDecimal divide = totalOriginPrice
                .divide(conditionMoney, RoundingMode.FLOOR)
                .setScale(0, RoundingMode.FLOOR);
        if (divide.compareTo(BigDecimal.ONE) > -1) {
            return discountMoney.multiply(divide);
        }
        return BigDecimal.ZERO;
    }
}
